<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>



    <script>

                let obj={
                    array:[1,2,3,4],
                    object:{a:4},
                    func () {
                        return 'func'
                    },
                    arrayObject:[{c:5},{d:6}],
                    date:new Date(),
                    set:new Set([7,8,9]),
                    map:new Map([
                        [11,'f'],
                        [12,'g']
                    ]),
                    reg:/[h-z]/

                }

                const objObg={
                    circluar:obj
                }
                obj.circluar=objObg

                function deepCloen(obj){
                    if(obj===null || typeof obj!=='object'){
                          return obj
                    }

                   
                    //计算并且记住结果
                   
                    //  if (!cache.has(obj)) {
                    //     caches = new Map()
                    //  }else{
                    //         return caches.get(obj);
                    //  }
                      
                    
                    if(!deepCloen.caches){
                        deepCloen.caches=new WeakMap()
                         console.log('pppppppp')
                           console.log(deepCloen.caches)
                    }
                    if(deepCloen.caches.has(obj)){
                        //    console.log('pppppppp')
                        //    console.log(deepCloen.caches)
                            return deepCloen.caches.get(obj)
                    } 


                    if(obj instanceof Map){
                         let temp=new Map()
                         deepCloen.caches.set(obj,temp)
                         for(let [key,value] of obj){
                            temp.set(key, deepCloen(value))
                         }
                         return temp
                    }else if(obj instanceof Set){
                         let temp=new Set()
                         deepCloen.caches.set(obj,temp)
                         for(let value of obj){
                            temp.add(deepCloen(value))
                         }
                         return temp
                    }else if(obj instanceof RegExp){
                         let temp=new RegExp(obj)
                         deepCloen.caches.set(obj,temp)
                         return temp
                    }else{
                        let temp=new obj.constructor()
                        deepCloen.caches.set(obj,temp)
                        for(let key in obj){
                            temp[key]=deepCloen(obj[key])
                        }
                        return temp

                    }
                    

                }

                let cloenData=deepCloen(obj)
                cloenData.object.a=100
                cloenData.array.push(520)
                console.log(obj)
                console.log(cloenData)


             




















    </script>
    
</body>
</html>